package cn.mooncode.basketballManage.controller;


import cn.mooncode.basketballManage.pojo.*;
import cn.mooncode.basketballManage.service.ITeamPlayerService;
import cn.mooncode.basketballManage.service.ITeamService;
import cn.mooncode.basketballManage.utils.UserUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jwz
 * @since 2021-03-28
 */
@RestController
@RequestMapping("/team")
public class TeamController {

    @Autowired
    private ITeamService teamService;
    @Autowired
    private ITeamPlayerService teamPlayerService;

    @ApiOperation(value = "添加球队信息")
    @PostMapping("/")
    public RespBean addTeam(@RequestBody Team team) {
        team.setCreateTime(LocalDateTime.now());
        team.setWinNum(0);
        team.setCid(0);
        if (teamService.save(team)) {
            return RespBean.success("添加成功！");
        } else {
            return RespBean.error("添加失败！");
        }
    }

    @ApiOperation(value = "添加教练信息")
    @PostMapping("/coachInfo")
    public RespBean addCoachInfo(@RequestParam Integer cid, @RequestParam Integer tid) {
        return teamService.addCoachInfo(cid, tid);
    }

    @ApiOperation(value = "删除教练信息")
    @PostMapping("/deleteCoach")
    public RespBean deleteCoach(@RequestBody Team team) {
        team.setCid(0);
        if (teamService.updateById(team)) {
            return RespBean.success("删除教练成功！");
        }
        return RespBean.error("删除教练失败！");
    }

    @ApiOperation(value = "添加球员信息")
    @PostMapping("/playerInfo")
    public RespBean addPlayerInfo(@RequestBody AddPlayerParam addPlayerParam) {
        return teamService.addPlayerInfo(addPlayerParam.getPlayerinfoList(), addPlayerParam.getTid());
    }

    @ApiOperation(value = "删除球员信息")
    @PostMapping("/deletePlayer")
    public RespBean deletePlayer(Integer pid, Integer tid) {
        if (teamPlayerService.remove(new QueryWrapper<TeamPlayer>().eq("pid", pid).eq("tid", tid))) {
            return RespBean.success("删除球员成功！");
        }
        return RespBean.error("删除球员失败！");
    }

    @ApiOperation(value = "获取所有球队信息")
    @GetMapping("/")
    public RespPageBean getAllTeams(@RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "2") Integer size
            , Team team) {
        return teamService.getAllTeams(currentPage, size, team);
    }

    @ApiOperation(value = "获取用户当前球队")
    @GetMapping("/currentTeam")
    public Team getCurrentTeam(Integer rid) {
//        rid用于判断当前用户是普通用户还是教练
        Integer uid = UserUtils.getUser().getId();
        return teamService.getCurrentTeam(uid, rid);
    }

    @ApiOperation(value = "获取除本身以外的球队信息")
    @GetMapping("/getTeam")
    public RespBean getTeam(Integer tid) {
        return teamService.getTeam(tid);
    }

    @ApiOperation(value = "修改球队信息")
    @PutMapping("/")
    public RespBean updateTeam(@RequestBody Team team) {
        if (teamService.updateById(team)) {
            return RespBean.success("修改成功！");
        }
        return RespBean.error("修改失败！");
    }

    @ApiOperation(value = "删除球队信息")
    @DeleteMapping("/{id}")
    @Transactional
    public RespBean deleteTeam(@PathVariable Integer id) {
        if (teamService.removeById(id)) {
            //        删除教练球员表中相应的数据
            if (teamPlayerService.list(new QueryWrapper<TeamPlayer>().eq("tid", id)).isEmpty()
                    || teamPlayerService.remove(new QueryWrapper<TeamPlayer>().eq("tid", id))) {
                return RespBean.success("删除成功！");
            }

        }
        return RespBean.error("删除失败！");
    }

    @ApiOperation(value = "修改球队获胜次数")
    @PostMapping("/winNum")
    public RespBean updateWinNum(@RequestParam Integer tid) {
        return teamService.updateWinNum(tid);
    }
}
